package com.xsmart.goods.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.goods.dao.BrandDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BrandService {

    @Autowired
    BrandDao brandDao;

    /**
     * 查询商品品牌
     * @param requestBean
     * @return
     */
    public ResponseBean queryBrand(RequestBean requestBean) {
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        String brand_name = requestBean.getRequestParamString("brand_name");
        Integer audit_status = requestBean.getRequestParamInteger("audit_status");
        Map<String, Object> brandMap = new HashMap<>();
        brandMap.put("brand_name",brand_name);
        brandMap.put("audit_status",audit_status);
        ResponseBean responseBean = requestBean.getResponseBean();
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        page.setRecords(brandDao.queryBrand(page,brandMap));
        responseBean.addResultPageItem("brandList", page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 新增商品品牌
     * @param requestBean
     * @return
     */
    public ResponseBean addBrand(RequestBean requestBean) {
        Map<String, Object> brandMap = getParam(requestBean);
        ResponseBean responseBean = requestBean.getResponseBean();
        int num = brandDao.addBrand(brandMap);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 修改商品品牌
     * @param requestBean
     * @return
     */
    public ResponseBean modBrand(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Map<String, Object> brandMap = getParam(requestBean);
        int num = brandDao.modBrand(brandMap);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 删除商品品牌
     * @param requestBean
     * @return
     */
    public ResponseBean delBrand(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer brand_id = requestBean.getRequestParamInteger("brand_id");
        int num = brandDao.delBrand(brand_id);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 查询所有的品牌
     * @param requestBean
     * @return
     */
    public ResponseBean queryAllBrand(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        List<Map<String,Object>> brandList = brandDao.queryAllBrand();
        responseBean.addResultItem("brandList", brandList);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }
    /**
     * 获取参数
     * @param requestBean
     * @return
     */
    private Map<String, Object> getParam(RequestBean requestBean){
        String[][] params = new String[][]{{"brand_name", ""},
                {"brand_letter", ""},
                {"brand_logo", ""},
                {"brand_desc", ""},
                {"site_url", ""},
                {"sort_order", "100"},
                {"is_show", "1"},
                {"is_delete", "1"},
                {"brand_id", ""},
                {"add_user_id", ""}};
        Map<String, Object> brandMap = new HashMap<>();
        for (String[] param : params) {
            Object value = requestBean.getRequestParam(param[0]);
            if (value == null) {
                brandMap.put(param[0], param[1]);
            } else {
                brandMap.put(param[0], requestBean.getRequestParam(param[0]));
            }
        }
        return brandMap;
    }
}
